﻿#region File Header
/***********************************************************************
 * Copyright © 2013 Beals Software
 * All Rights Reserved
************************************************************************
Author: Donald Beals
Date: February 4th, 2013
Description: TODO: Write a description of this file here.
****************************** Change Log ******************************
02.04.13 - Created initial file. (dbeals)
***********************************************************************/
#endregion

#region Using Statements
using System;
using System.IO;
using System.Net;
#endregion

namespace GameJolt.Requests
{
	/// <summary>
	/// 
	/// </summary>
	internal abstract class GenericRequestBase<TResult> : WebRequestBase<TResult>
	{
		#region Constructors
		protected GenericRequestBase(string gameID, string privateKey)
			: base(gameID, privateKey)
		{
		}
		#endregion

		#region Methods
		protected abstract TResult ProcessResponseData(string data);

		public override TResult Process(IAsyncResult result)
		{
			base.Process(result);
			var fullUrl = this.GetFullUrl();
			var request = (HttpWebRequest)WebRequest.Create(fullUrl);
			request.Method = "GET";
			try
			{
				using(var response = request.GetResponse())
				{
					using(var streamReader = new StreamReader(response.GetResponseStream()))
					{
						var data = streamReader.ReadToEnd();
						return ProcessResponseData(data);
					}
				}
			}
			catch(WebException)
			{
				return default(TResult);
			}
		}
		#endregion
	}
}
